package com.belazy.library.util.file;

import cn.hutool.core.io.IoUtil;
import org.springframework.core.io.ClassPathResource;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


/**
 * @Description <p> Resources文件工具 </p>
 * @Author chupeng.tang
 * @CreateTime 2023/5/19 17:29
 * @Version 1.0
 */
public class ResourcesUtil {
    /**
     * 获取resource下文件流
     * @param filePath
     * @return
     * @throws FileNotFoundException
     */
    public static InputStream getResourcesAsStream(String filePath) throws FileNotFoundException {
        try {
            ClassPathResource resource = new ClassPathResource (filePath);
            return resource.getInputStream();
        } catch (IOException e) {
            throw new FileNotFoundException(filePath);
        }

    }

    /**
     * 获取resource下文件内容
     * @param filePath
     * @return String
     */
    public static String getResourcesByStream(String filePath) {
        String str = "";
        ClassPathResource resource = new ClassPathResource (filePath);
        try {
            InputStream inputStream = resource.getInputStream ();
            str = IoUtil.read (inputStream, "UTF-8");
        } catch (IOException e) {
            e.printStackTrace ();
        }
        return str;
    }
}
